package com.example.applicationview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.applicationview.bean.PieData;
import com.example.applicationview.view.PathFillTypeView;
import com.example.applicationview.view.PieView;
import com.example.applicationview.view.PieView2;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 橡皮擦画图，实现脱衣服效果
 */
public class RubberActivity extends AppCompatActivity implements View.OnTouchListener {

    @BindView(R.id.img_after)
    ImageView img_after;
    @BindView(R.id.img_before)
    ImageView img_before;
    @BindView(R.id.pie_view)
    PieView pieView;
    @BindView(R.id.pie_view2)
    PieView2 pieView2;
    @BindView(R.id.pathFillType_View)
    PathFillTypeView pathFillTypeView;
//    @BindView(R.id.button)
//    Button button;

    //前景bitmap
    private Bitmap alterBitmap;
    //绘制前景bitmap的 画布
    private Canvas canvas;
    private Paint paint;

    private Bitmap after;
    private Bitmap before;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rubber_image);
        ButterKnife.bind(this);

        bindViews();
        pieViewData();
    }

    private void pieViewData() {
        ArrayList<PieData> mData = new ArrayList<>();
        PieData data1 = new PieData("haha", 10);
        PieData data2 = new PieData("haha", 20);
        PieData data3 = new PieData("haha", 20);
        PieData data4 = new PieData("haha", 20);
        PieData data5 = new PieData("haha", 30);
        mData.add(data1);
        mData.add(data2);
        mData.add(data3);
        mData.add(data4);
        mData.add(data5);

        pieView.setStartAngle(0);
        pieView.setData(mData);
        pieView2.setStartAngle(0);
        pieView2.setData(mData);
    }

    private void bindViews() {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 1;
        after = BitmapFactory.decodeResource(getResources(), R.mipmap.image_after, opts);
        before = BitmapFactory.decodeResource(getResources(), R.mipmap.image_before, opts);
        //定义出来的是只读图片
        alterBitmap = Bitmap.createBitmap(before.getWidth(), before.getHeight(), Bitmap.Config.ARGB_4444);
        //创建画布
        canvas = new Canvas(alterBitmap);
        paint = new Paint();
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(5);
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
        //将before画在 alterBitmap上
        canvas.drawBitmap(before, new Matrix(), paint);

        img_after.setImageBitmap(after);
        img_before.setImageBitmap(before);

        img_before.setOnTouchListener(this);

    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int newX = (int) event.getX();
                int newY = (int) event.getY();
                //setPixel方法是将某一个像素点设置成一个颜色，而这里我们把他设置成透明
                //另外通过嵌套for循环将手指触摸区域的20*20个像素点设置为透明
                for (int i = -20; i < 20; i++) {
                    for (int j = -20; j < 20; j++) {
                        if (i + newX >= 0 && j + newY >= 0 && i + newX < before.getWidth() && j + newY < before.getHeight())
                            alterBitmap.setPixel(i + newX, j + newY, Color.TRANSPARENT);
                    }
                }

                img_before.setImageBitmap(alterBitmap);
                break;
        }
        return true;
    }

    public void click(View view) {
        String change = pathFillTypeView.change();
//        button.setText(change);
    }
}
